import { defineStore } from 'pinia'
import type { TeacherPairRelation } from '../types/TeacherPairRelation'

/**
 * 教师结对关系存储模块
 * 负责管理教师结对关系的全局状态
 */
export const useTeacherPairRelationStore = defineStore('teacherPairRelation', {
    state: () => ({
        // 所有结对关系列表
        relations: [] as TeacherPairRelation[],
        // 当前选中的关系
        currentRelation: null as TeacherPairRelation | null,
        // 加载状态
        loading: false,
        // 筛选条件
        filter: {
            pairType: null as number | null,
            status: null as number | null
        }
    }),
    actions: {
        /**
         * 获取所有结对关系
         */
        async fetchAllRelations() {
            this.loading = true
            try {
                // this.relations = await relationApi.getAllTeacherPairRelations()
            } finally {
                this.loading = false
            }
        },

        /**
         * 创建新的结对关系
         * @param newRelation - 新结对关系对象
         */
        async createRelation(newRelation: Omit<TeacherPairRelation, 'relationId'>) {
            this.loading = true
            try {
                // const created = await relationApi.createTeacherPairRelation(newRelation)
                // this.relations.push(created)
            } finally {
                this.loading = false
            }
        },

        /**
         * 更新结对关系状态
         * @param relationId - 关系ID
         * @param newStatus - 新状态(0-进行中,1-已结束)
         */
        async updateRelationStatus(relationId: number, newStatus: number) {
            this.loading = true
            try {
                // await relationApi.updateTeacherPairRelationStatus(relationId, newStatus)
                // const relation = this.relations.find(r => r.relationId === relationId)
                // if (relation) {
                //     relation.status = newStatus
                // }
            } finally {
                this.loading = false
            }
        },

        /**
         * 设置结对类型筛选
         * @param pairType - 结对类型(1-老带新帮扶,2-科技干部结对,3-新老师导师指导)
         */
        setPairTypeFilter(pairType: number | null) {
            this.filter.pairType = pairType
        },

        /**
         * 设置状态筛选
         * @param status - 状态(0-进行中,1-已结束)
         */
        setStatusFilter(status: number | null) {
            this.filter.status = status
        }
    },
    getters: {
        /**
         * 获取筛选后的结对关系
         */
        filteredRelations(state): TeacherPairRelation[] {
            let result = [...state.relations]

            if (state.filter.pairType !== null) {
                result = result.filter(r => r.pairType === state.filter.pairType)
            }

            if (state.filter.status !== null) {
                result = result.filter(r => r.status === state.filter.status)
            }

            return result
        },

        /**
         * 获取进行中的结对关系
         */
        activeRelations(state): TeacherPairRelation[] {
            return state.relations.filter(r => r.status === 0)
        },

        /**
         * 按结对类型分组的统计
         */
        relationsByType(state) {
            const stats = new Map<number, number>()
            state.relations.forEach(relation => {
                stats.set(relation.pairType, (stats.get(relation.pairType) || 0) + 1)
            })
            return Array.from(stats.entries()).map(([type, count]) => ({
                type,
                count
            }))
        }
    }
})
